App.define("strTool", {
    wrap: function (str, expectStr, num) {
        var headStr = "";
        for (var i = 0; i < num - 1; i++) {
            headStr += expectStr;
        }
        return (headStr + str).slice(-num);
    },
    
    computedBlueByDevice: function (device) {
        var code = device.code.slice(-6);
        return {
            name: device.type.toUpperCase() === "8B22" ? device.type + code : "CLW-BJQ000",
            mac: code.replace(/\w{2}/g, function (match) {
                return match + ":";
            }) + "50:a0:00"
        }
    },
    createQRCode: function (val) {
        var base64str = val.split("?")[1].slice(2);
        var id = atob(base64str);
        return id + this._getVerticeCode(id.slice(-10));
    },
    _getVerticeCode: function (str) {
        var numList = str.split("").map(function (item) { return parseInt(item); });
        var codeList = [7, 6, 4, 6, 2, 1, 9, 8, 5, 3];
        var num = 0;
        for (var i = 0; i < 10; i++) {
            num += numList[i] * codeList[i];
        }
        return num % 10;
    },
    checkVehicleCode: function (str) {
        var numList = str.slice(4, -1).split("").map(function (item) { return parseInt(item); });
        var lastStr = str.slice(-1);
        var codeList = [7, 6, 4, 6, 2, 1, 9, 8, 5, 3];
        var num = 0;
        for (var i = 0; i < 10; i++) {
            num += numList[i] * codeList[i];
        }
        return (num % 10) == lastStr;
    },
    brandGBToSelectObj: function (data) {
        function contains(modelList, value) {
            for (var i = 0; i < modelList.length; i++) {
                var model = modelList[i];
                if (model.model == value) return true;
            }
            return false;
        }
        return {
            getOriginData: function (value) {
                value = value || "";
                var obj1 = {
                        items: []
                    },
                    obj2 = {
                        items: []
                    };
                for (var i = 0; i < data.length; i++) {
                    var d = data[i];
                    obj1.items.push({
                        content: d.brandName,
                        value: d.brandName
                    });
                    if (value) {
                        if (contains(d.modelList, value)) {
                            obj1.index = i + 1;
                            var list = d.modelList;
                            for (var j = 0; j < list.length; j++) {
                                var dd = list[j];
                                obj2.items.push({
                                    content: dd.model,
                                    value: dd.model,
                                    maxSpeed: dd.maxSpeed,
                                    volume: dd.volume,
                                    weight: dd.weight
                                });
                                if (value === dd.model) obj2.index = j + 1;
                            }
                        }
                    }
                }

                if (!value) {
                    obj1.index = 1;
                    var list = data[0].modelList;
                    for (var j = 0; j < list.length; j++) {
                        var dd = list[j];
                        obj2.items.push({
                            content: dd.model,
                            value: dd.model,
                            maxSpeed: dd.maxSpeed,
                            volume: dd.volume,
                            weight: dd.weight
                        });
                         obj2.index = 1;
                    }
                }
                return [obj1, obj2];
            },
            getNextData: function (value) {
                var result = [];
                for (var i = 0; i < data.length; i++) {
                    if (data[i].brandName === value) {
                        list = data[i].modelList;
                        for (var j = 0; j < list.length; j++) {
                            var dd = list[j];
                            result.push({
                                content: dd.model,
                                value: dd.model,
                                maxSpeed: dd.maxSpeed,
                                volume: dd.volume,
                                weight: dd.weight
                            });
                        }
                        break;
                    }
                }
                return result;
            }
        }
    },
    base64ToDevice: function (str) {
        var unicodeStr = atob(str),
            n = unicodeStr.length;
        var list = [];
        for (var i = 0; i < n; i++) {
            list.push(unicodeStr.charCodeAt(i).toString(16));
        }

        return {
            type: this.wrap(list[0], "0", 2) + this.wrap(list[1], "0", 2),
            code: this.wrap(list[3], "0", 2) + this.wrap(list[4], "0", 2) + this.wrap(list[5], "0", 2)
        }
    },
    isTest: function (url) {
        return /localhost|10\.130\.0\.108/.test(url);
    },
    computeStrSortList: function (list) {
        var pyFirstWord = ["阿", "八", "嚓", "搭", "额", "发", "呷", "哈", "即", "咔", "拉", "呣", "那", "嚄", "扒", "七", "然", "仨", "他", "挖", "兮", "丫", "匝"];
        var lastWord = "糳";
        var pyFirstLeter = ["A", "B", "C", "D", "E", "F", "G", "H", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "W", "X", "Y", "Z", "其它"];

        var splitList = [];
        for (var i = 0; i < pyFirstLeter.length; i++) {
            splitList.push([]);
        }

        for (var i = 0; i < list.length; i++) {
            var tword = list[i].content.slice(0, 1);
            for (var j = 0; j < pyFirstWord.length; j++) {
                var word = pyFirstWord[j];

                if (tword.localeCompare(word) >= 0) {
                    if (pyFirstWord.length - 1 == j) {
                        if (lastWord.localeCompare(tword) <= 0) splitList[j + 1].push(list[i]);
                        else splitList[j].push(list[i]);
                    } else continue;
                } else {
                    if (j == 0) {
                        splitList[pyFirstWord.length].push(list[i]);
                    } else {
                        splitList[j - 1].push(list[i]);
                    }
                    break;
                }
            }
        }

        var lastList = [],
            lastletters = [],
            lastWords = [];
        for (var i = 0; i < splitList.length; i++) {
            if (splitList[i].length > 0) {
                lastList.push(splitList[i].sort(function (a, b) {
                    return a.content.localeCompare(b.content);
                }));
                lastletters.push(pyFirstLeter[i]);
                if (pyFirstWord[i]) lastWords.push(pyFirstWord[i]);
            }
        }

        return {
            getOriginData: function (value) {
                var obj1 = {
                        index: 0,
                        items: []
                    },
                    obj2 = {
                        index: 0,
                        items: []
                    };
                for (var i = 0; i < lastletters.length; i++) {
                    obj1.items.push({
                        value: lastletters[i],
                        content: lastletters[i]
                    });
                }
                if (value) {
                    for (var j = 0; j < lastWords.length; j++) {
                        if (value.localeCompare(lastWords[j]) > 0) {
                            if (lastWords.length - 1 == j) {
                                if (lastWord.localeCompare(value) <= 0) {
                                    obj2.items = lastList[j + 1];
                                    obj1.index = j + 2;
                                } else {
                                    obj2.items = lastList[j];
                                    obj1.index = j + 1;
                                }
                            } else continue
                        } else {
                            if (j == 0) {
                                obj2.items = lastList[lastWords.length];
                                obj1.index = lastWords.length + 1;
                            } else {
                                obj2.items = lastList[j - 1];
                                obj1.index = j;
                            }

                            break;
                        }
                    }
                    var items = obj2.items;
                    for (var i = 0; i < items.length; i++) {
                        if (items[i].content == value) {
                            obj2.index = i + 1;
                            break;
                        }
                    }
                } else {
                    obj2.items = lastList[0];
                }
                return [obj1, obj2];
            },
            getNextData: function (value) {
                var index = lastletters.indexOf(value);
                return lastList[index];
            }
        }
    }
})